package com.xgkj.diyiketang.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.BuyGuankanquanInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.BuildOrderTask;
import com.xgkj.diyiketang.task.BuyGuankanquanTask;
import com.xgkj.diyiketang.task.GetUserDetailTask;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.WeiXinPay;
import com.xgkj.diyiketang.wxapi.WXPayEntryActivity;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class ZhuanyunMoneyActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout ming_e, class_num, shenqing_school, shenfenid;
    private TextView buy, sell, ming_e_tv, class_num_tv, back, tody_price, title_bar, zhuanyun_shizhi, parent_shizhi;
    private AlertDialog mConfirmStopDialog;
    private PopupWindow qidaiPop;
    private int detailTaskId = -1;
    private int useMingeTask = -1;
    private EatShowUserInfo userInfo;
    private String zhuanyun_yz;
    private String zhuanyun_zr;
    private String zhuanyun_ds;
    private String zhuanyun_xy;
    private int vipCode;
    private PopupWindow setPop;
    private LinearLayout zhunayun_money, goumai, zhuanrang;
    private String minge = "";
    private Button btn;
    private String zhuanyun_price;
    private String parent_price;
    private String type;
    private Handler handler;
    private Runnable runnable;
    private ViewFlipper stepVvp;
    private PopupWindow goumai_zhuanrangPop;
    private int orderTaskId = -1;
    private String state;
    private String price;
    private String quotaSum;
    private String total_money;
    private String quotaId;
    private String orderNo;
    final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
    private WeixinReceive mWeiXinReceive;
    private String orderTitle = "第一课堂";
    private String orderBody = "第一课堂";
    private ProgressDialog mProgress;
    private String zhuanyun_jihuo;
    private String parent_jihuo;
    private String p_minge;
    private String z_minge;
    private String p_zhuanyun_yz;
    private String p_zhuanyun_zr;
    private String p_zhuanyun_ds;
    private String p_zhuanyun_xy;
    private int buyGuankanquanTask = -1;
    private List<BuyGuankanquanInfo.ListBean.BuyDateDetailBean> list;
    private List<String> text_list = new ArrayList<>();
    private String shenfen1;
    private String shenfen2;
    private String parent_shouyi;
    private String zhuanyun_shouyi;
    private LinearLayout parent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_zhuanyun_money);

        initUI();
        getPopwindow();
        getUserDetail();
        getBuyDetail();
//        getGoumai_zhuanPop();
    }

    private RelativeLayout dangqianshizhi, anxin, daai;

    private void initUI() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        parent = (LinearLayout) inflater.inflate(R.layout.activity_zhuanyun_money, null);
        title_bar = (TextView) parent.findViewById(R.id.title_bar);
        stepVvp = (ViewFlipper) parent.findViewById(R.id.step_vvp);
        zhuanyun_shizhi = (TextView) parent.findViewById(R.id.dangqianshizhi_go);
        goumai = (LinearLayout) parent.findViewById(R.id.zhuanyun_goumai);
        goumai.setOnClickListener(this);
        tody_price = (TextView) parent.findViewById(R.id.tody_price);
        zhuanrang = (LinearLayout) parent.findViewById(R.id.zhuanyun_zhuanrang);
        zhuanrang.setOnClickListener(this);
        btn = (Button) parent.findViewById(R.id.live_prepare_btn);
        btn.setOnClickListener(this);
        back = (TextView) parent.findViewById(R.id.zhuanyun_back);
        back.setOnClickListener(this);
        zhunayun_money = (LinearLayout) parent.findViewById(R.id.zhunayun_money);
        ming_e = (RelativeLayout) parent.findViewById(R.id.myflower_layout);
        class_num = (RelativeLayout) parent.findViewById(R.id.class_num_rl);
        shenqing_school = (RelativeLayout) parent.findViewById(R.id.shenqing_school_rl);
        shenfenid = (RelativeLayout) parent.findViewById(R.id.shenfencard_num_rl);
        ming_e_tv = (TextView) parent.findViewById(R.id.xiaohua);
        class_num_tv = (TextView) parent.findViewById(R.id.class_num_2);
        dangqianshizhi = (RelativeLayout) parent.findViewById(R.id.dangqianshizhi_rl);
        anxin = (RelativeLayout) parent.findViewById(R.id.aixinjuanzeng_rl);
        daai = (RelativeLayout) parent.findViewById(R.id.daaifenxiang_rl);
        setContentView(parent);
        dangqianshizhi.setOnClickListener(this);
        anxin.setOnClickListener(this);
        daai.setOnClickListener(this);
        ming_e.setOnClickListener(this);
        class_num.setOnClickListener(this);
        shenqing_school.setOnClickListener(this);
        shenfenid.setOnClickListener(this);
        back.setOnClickListener(this);
//        initDescPopWindow();
        Intent intent = getIntent();
        if (intent.getStringExtra("minge_yz") != null) {
            minge = intent.getStringExtra("minge_yz");
        } else if (intent.getStringExtra("minge_zr") != null) {
            minge = intent.getStringExtra("minge_yz");
        } else if (intent.getStringExtra("minge_ds") != null) {
            minge = intent.getStringExtra("minge_yz");
        } else if (intent.getStringExtra("minge_xy") != null) {
            minge = intent.getStringExtra("minge_yz");
        }
        if (intent.getStringExtra("type") != null) {
            type = intent.getStringExtra("type");
        }
        if (type.equals("zhuanyun")) {
            quotaId = "3d9513eb1b674af98de226b315765e3f";
            title_bar.setText("转运学院");
            if (intent.getStringExtra("zhuanyun_yz") != null) {
                zhuanyun_yz = intent.getStringExtra("zhuanyun_yz");
            }
            if (intent.getStringExtra("zhuanyun_zr") != null) {
                zhuanyun_zr = intent.getStringExtra("zhuanyun_zr");
            }
            if (intent.getStringExtra("zhuanyun_ds") != null) {
                zhuanyun_ds = intent.getStringExtra("zhuanyun_ds");
            }
            if (intent.getStringExtra("zhuanyun_xy") != null) {
                zhuanyun_xy = intent.getStringExtra("zhuanyun_xy");
            }
            if (intent.getIntExtra("vipCode", 0) != 0) {
                vipCode = intent.getExtras().getInt("vipCode");
            }
        } else if (type.equals("parent")) {
            title_bar.setText("父母大学");
            quotaId = "2737f7a418bf41c998728e6a0f9f253d";
            if (intent.getStringExtra("zhuanyun_yz") != null) {
                p_zhuanyun_yz = intent.getStringExtra("zhuanyun_yz");
            }
            if (intent.getStringExtra("zhuanyun_zr") != null) {
                p_zhuanyun_zr = intent.getStringExtra("zhuanyun_zr");
            }
            if (intent.getStringExtra("zhuanyun_ds") != null) {
                p_zhuanyun_ds = intent.getStringExtra("zhuanyun_ds");
            }
            if (intent.getStringExtra("zhuanyun_xy") != null) {
                p_zhuanyun_xy = intent.getStringExtra("zhuanyun_xy");
            }
            if (intent.getIntExtra("vipCode", 0) != 0) {
                vipCode = intent.getExtras().getInt("vipCode");
            }
        }
        ming_e_tv.setText(minge);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (EatShowApp.getACache().getAsString("pay_success") != null) {
            if (EatShowApp.getACache().getAsString("pay_success").equals("true")) {
                getUserDetail();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (setPop != null) {
            setPop.dismiss();
        }
        if (goumai_zhuanrangPop != null) {
            goumai_zhuanrangPop.dismiss();
        }
        if (qidaiPop != null) {
            qidaiPop.dismiss();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            if (setPop != null) {
//                if (type.equals("zhuanyun")&&!TextUtils.isEmpty(shenfen1)){
//                    parent.removeAllViews();
//                    initDescPopWindow();
////                    setPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
//                }else if (type.equals("parent")&&!TextUtils.isEmpty(shenfen2)){
//                    parent.removeAllViews();
//                    initDescPopWindow();
////                    setPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
//                }
            }
        }
    }

    private ImageView close_btn;

    private void initDescPopWindow() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.tongzhi_pop, null);
//        View view = LayoutInflater.from(mContext).inflate(R.layout.tongzhi_pop, null);
        TextView i_know = (TextView) view.findViewById(R.id.i_know);
        i_know.setOnClickListener(this);
        close_btn = (ImageView) view.findViewById(R.id.close_btn);
        close_btn.setOnClickListener(this);
        setPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        setPop.setBackgroundDrawable(new ColorDrawable());
        setPop.setFocusable(true);
        setPop.setOutsideTouchable(true);
        setPop.showAtLocation(view, Gravity.CENTER, 0, 0);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhuanyun_goumai:
                state = "0";
                getGoumai_zhuanPop();
                goumai_title.setText("购买");
                goumai_num_tv.setText("购买数量");
                goumai_price_tv.setText("购买单价");
                zhuangrang_price.setHint("购买单价需高于今日价");
                break;
            case R.id.zhuanyun_zhuanrang:
                state = "1";
                getGoumai_zhuanPop();
                goumai_title.setText("转让");
                goumai_num_tv.setText("转让数量");
                goumai_price_tv.setText("转让单价");
                zhuangrang_price.setHint("转让单价需低于今日价");
                break;
            //当前名额
            case R.id.myflower_layout:
                if (type.equals("zhuanyun")) {
                    if (TextUtils.isEmpty(shenfen1)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                    Intent intent = new Intent(this, MyWatchPageActivity.class);
                    intent.putExtra("type", type);
                    intent.putExtra("jihuominge", zhuanyun_jihuo);
                    intent.putExtra("minge", z_minge);
                    startActivity(intent);

                } else if (type.equals("parent")) {
                    if (TextUtils.isEmpty(shenfen2)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                    Intent intent = new Intent(this, MyWatchPageActivity.class);
                    intent.putExtra("type", type);
                    intent.putExtra("jihuominge", parent_jihuo);
                    intent.putExtra("minge", p_minge);
                    startActivity(intent);
                }
                break;
            case R.id.close_btn:
                setPop.dismiss();
                break;
            case R.id.i_know:
                Intent intent = new Intent(this, ZhuanyunOrderActivity.class);
                if (type.equals("zhuanyun")) {
                    intent.putExtra("type", type);
                    intent.putExtra("vipcode", vipCode);
                    intent.putExtra("zhuanyun_yz", zhuanyun_yz);
                    intent.putExtra("zhuanyun_zr", zhuanyun_zr);
                    intent.putExtra("zhuanyun_ds", zhuanyun_ds);
                    intent.putExtra("zhuanyun_xy", zhuanyun_xy);
                    if (TextUtils.isEmpty(zhuanyun_yz) || TextUtils.isEmpty(zhuanyun_zr) || TextUtils.isEmpty(zhuanyun_ds) || TextUtils.isEmpty(zhuanyun_xy)) {
                        ToastUtil.showMessage(this, "您还暂无身份");
                        return;
                    }
                } else {
                    intent.putExtra("type", type);
                    intent.putExtra("vipcode", vipCode);
                    intent.putExtra("zhuanyun_yz", p_zhuanyun_yz);
                    intent.putExtra("zhuanyun_zr", p_zhuanyun_zr);
                    intent.putExtra("zhuanyun_ds", p_zhuanyun_ds);
                    intent.putExtra("zhuanyun_xy", p_zhuanyun_xy);
                    if (TextUtils.isEmpty(p_zhuanyun_yz) || TextUtils.isEmpty(p_zhuanyun_zr) || TextUtils.isEmpty(p_zhuanyun_ds) || TextUtils.isEmpty(p_zhuanyun_xy)) {
                        ToastUtil.showMessage(this, "您还暂无身份");
                        return;
                    }
                }
                startActivity(intent);
                break;
            case R.id.dangqianshizhi_rl:
                if (type.equals("zhuanyun")) {
                    if (TextUtils.isEmpty(shenfen1)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                } else if (type.equals("parent")) {
                    if (TextUtils.isEmpty(shenfen2)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                }
                break;
            case R.id.aixinjuanzeng_rl:
                if (type.equals("zhuanyun")) {
                    if (TextUtils.isEmpty(shenfen1)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                } else if (type.equals("parent")) {
                    if (TextUtils.isEmpty(shenfen2)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                }
                break;
            case R.id.daaifenxiang_rl:
                if (type.equals("zhuanyun")) {
                    if (TextUtils.isEmpty(shenfen1)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                } else if (type.equals("parent")) {
                    if (TextUtils.isEmpty(shenfen2)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                }
                break;
            case R.id.zhuanyun_back:
                finish();
                break;
            //使用名额
            case R.id.live_prepare_btn:

                break;
            //累计收入
            case R.id.class_num_rl:
                if (type.equals("zhuanyun")) {
                    if (TextUtils.isEmpty(shenfen1)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                } else if (type.equals("parent")) {
                    if (TextUtils.isEmpty(shenfen2)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    }
                }
                break;
            //更多身份
            case R.id.shenqing_school_rl:
                intent = new Intent(this, ZhunyunSchoolActivity.class);
                if (type.equals("zhuanyun")) {
                    intent.putExtra("type", type);
                    intent.putExtra("vipcode", vipCode);
                    intent.putExtra("zhuanyun_yz", zhuanyun_yz);
                    intent.putExtra("zhuanyun_zr", zhuanyun_zr);
                    intent.putExtra("zhuanyun_ds", zhuanyun_ds);
                    intent.putExtra("zhuanyun_xy", zhuanyun_xy);
                } else {
                    intent.putExtra("type", type);
                    intent.putExtra("vipcode", vipCode);
                    intent.putExtra("zhuanyun_yz", p_zhuanyun_yz);
                    intent.putExtra("zhuanyun_zr", p_zhuanyun_zr);
                    intent.putExtra("zhuanyun_ds", p_zhuanyun_ds);
                    intent.putExtra("zhuanyun_xy", p_zhuanyun_xy);
                }
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            //身份编号
            case R.id.shenfencard_num_rl:
                intent = new Intent(this, ZhuanyunOrderActivity.class);
                if (type.equals("zhuanyun")) {
                    if (TextUtils.isEmpty(shenfen1)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    } else {
                        intent.putExtra("type", type);
                        intent.putExtra("vipcode", vipCode);
                        intent.putExtra("zhuanyun_yz", zhuanyun_yz);
                        intent.putExtra("zhuanyun_zr", zhuanyun_zr);
                        intent.putExtra("zhuanyun_ds", zhuanyun_ds);
                        intent.putExtra("zhuanyun_xy", zhuanyun_xy);
                    }
                } else if (type.equals("parent")) {
                    if (TextUtils.isEmpty(shenfen2)) {
//                        getPopwindow();
                        qidaiPop.showAtLocation(zhunayun_money, Gravity.CENTER, 0, 0);
                        return;
                    } else {
                        intent.putExtra("type", type);
                        intent.putExtra("vipcode", vipCode);
                        intent.putExtra("zhuanyun_yz", p_zhuanyun_yz);
                        intent.putExtra("zhuanyun_zr", p_zhuanyun_zr);
                        intent.putExtra("zhuanyun_ds", p_zhuanyun_ds);
                        intent.putExtra("zhuanyun_xy", p_zhuanyun_xy);
                    }
                }
                startActivity(intent);
                break;
            case R.id.ok_btn:
                if (qidaiPop != null) {
                    qidaiPop.dismiss();
                    WindowManager.LayoutParams lp = getWindow().getAttributes();
                    lp.alpha = 1f; //0.0-1.0
                    getWindow().setAttributes(lp);
//                    qidaiPop.setBackgroundDrawable();
                }
                break;
            case R.id.goumai_queding:
                price = zhuangrang_price.getText().toString();
                quotaSum = zhuanrang_num.getText().toString();
                if (TextUtils.isEmpty(price)) {
                    ToastUtil.showMessage(this, "请输入价钱");
                    return;
                }
                double i1 = Double.parseDouble(price);
                int i3 = Integer.parseInt(quotaSum);
                total_money = (i1 * i3) + "";
                if (type.equals("zhuanyun")) {
                    if (state.equals("0")) {
                        if (i1 > Double.parseDouble(zhuanyun_price)) {
                            getOrder();
                        } else {
                            ToastUtil.showMessage(this, "购买价需大于今日价");
                        }
                    } else if (state.equals("1")) {
                        if (i1 < Double.parseDouble(zhuanyun_price)) {
                            if (zhuanyun_jihuo != null) {
                                if (Integer.parseInt(zhuanyun_jihuo) > Integer.parseInt(quotaSum)) {
                                    getOrder();
                                } else {
                                    ToastUtil.showMessage(this, "转让观看券已经超出你所拥有的激活观看券");
                                }
                            } else {
                                ToastUtil.showMessage(this, "您还没有激活观看券，暂不能转让");
                            }

                        } else {
                            ToastUtil.showMessage(this, "转让价需小于今日价");
                        }
                    }
                } else if (type.equals("parent")) {
                    if (state.equals("0")) {
                        if (i1 > Double.parseDouble(parent_price)) {
                            getOrder();
                        } else {
                            ToastUtil.showMessage(this, "购买价需大于今日价");
                        }
                    } else if (state.equals("1")) {
                        if (i1 < Double.parseDouble(parent_price)) {
                            if (parent_jihuo != null) {
                                if (Integer.parseInt(parent_jihuo) > Integer.parseInt(quotaSum)) {
                                    getOrder();
                                } else {
                                    ToastUtil.showMessage(this, "转让观看券已经超出你所拥有的激活观看券");
                                }
                            } else {
                                ToastUtil.showMessage(this, "您还没有激活观看券，暂不能转让");
                            }
                        } else {
                            ToastUtil.showMessage(this, "转让价需小于今日价");
                        }
                    }
                }
                break;
            case R.id.goumai_cancle:
                if (goumai_zhuanrangPop != null) {
                    goumai_zhuanrangPop.dismiss();
                }
                break;
            case R.id.goumai_add:
                if (TextUtils.isEmpty(zhuanrang_num.getText())) {
                    ToastUtil.showMessage(this, "数值不能为空");
                } else {
                    String s = zhuanrang_num.getText().toString();
                    int i = Integer.parseInt(s);
                    zhuanrang_num.setText(i + 1 + "");
                }
                break;
            case R.id.goumai_lose:
                if (TextUtils.isEmpty(zhuanrang_num.getText())) {
                    ToastUtil.showMessage(this, "数值不能为空");
                } else {
                    String s2 = zhuanrang_num.getText().toString();
                    int i2 = Integer.parseInt(s2);
                    if (i2 < 2) {
                        ToastUtil.showMessage(this, "最小值为1");
                    } else {
                        zhuanrang_num.setText(i2 - 1 + "");
                    }
                }
                break;
        }
    }

    private EditText zhuanrang_num, zhuangrang_price;
    private TextView price_heji, queding, quxiao, goumai_title, goumai_num_tv, goumai_price_tv;
    private ImageView jian_img;
    private RelativeLayout add_img;

    private void getGoumai_zhuanPop() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.goumai_zhuanrang_pop, null);
        goumai_title = (TextView) inflate.findViewById(R.id.goumai_title);
        goumai_num_tv = (TextView) inflate.findViewById(R.id.goumai_num);
        goumai_price_tv = (TextView) inflate.findViewById(R.id.goumai_price_tv);
        zhuanrang_num = (EditText) inflate.findViewById(R.id.goumai_num_et);
        zhuangrang_price = (EditText) inflate.findViewById(R.id.goumai_price);
        price_heji = (TextView) inflate.findViewById(R.id.price_heji);
        queding = (TextView) inflate.findViewById(R.id.goumai_queding);
        quxiao = (TextView) inflate.findViewById(R.id.goumai_cancle);
        add_img = (RelativeLayout) inflate.findViewById(R.id.goumai_add);
        jian_img = (ImageView) inflate.findViewById(R.id.goumai_lose);
        queding.setOnClickListener(this);
        quxiao.setOnClickListener(this);
        add_img.setOnClickListener(this);
        jian_img.setOnClickListener(this);
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        goumai_zhuanrangPop = new PopupWindow(inflate, width - 60, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        goumai_zhuanrangPop.setBackgroundDrawable(new ColorDrawable());
        goumai_zhuanrangPop.setTouchable(true);
        goumai_zhuanrangPop.setOutsideTouchable(false);
        goumai_zhuanrangPop.setFocusable(true);
        goumai_zhuanrangPop.showAtLocation(inflate, Gravity.CENTER, 0, 0);
        zhuanrang_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(zhuangrang_price.getText().toString())) {
                    if (TextUtils.isEmpty(s)) {
                        s = "0";
                    }
                    double i1 = Double.parseDouble(zhuangrang_price.getText().toString());
                    int i3 = Integer.parseInt(s.toString());
                    total_money = (i1 * i3) + "";
                    price_heji.setText("合计：" + total_money);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        zhuangrang_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(zhuanrang_num.getText().toString())) {
                    if (TextUtils.isEmpty(s)) {
                        s = "0";
                    }
                    double i1 = Double.parseDouble(s.toString());
                    int i3 = Integer.parseInt(zhuanrang_num.getText().toString());
                    total_money = (i1 * i3) + "";
                    price_heji.setText("合计：" + total_money);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private TextView ok_btn;

    private void getPopwindow() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.item_qidai_pop, null);
        ok_btn = (TextView) inflate.findViewById(R.id.ok_btn);
        ok_btn.setOnClickListener(this);
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        qidaiPop = new PopupWindow(inflate, width / 2 + 100, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f; //0.0-1.0
//        getWindow().setAttributes(lp);
//        ColorDrawable dw = new ColorDrawable(0x60000000);
        qidaiPop.setBackgroundDrawable(new ColorDrawable());
        qidaiPop.setTouchable(true);
        qidaiPop.setOutsideTouchable(false);
        qidaiPop.setFocusable(true);

    }

    //购买名额
    private void getOrder() {
        BuildOrderTask orderTask = new BuildOrderTask(null, "6", total_money, null, null, null, null, quotaId, state, price, quotaSum);
        orderTask.addListener(new MyListener(mContext));
        orderTaskId = getEatShowApp().getTaskManager().addTask(orderTask);
    }

    private void getUserDetail() {
        GetUserDetailTask task = new GetUserDetailTask();
        task.addListener(new MyListener(mContext));
        detailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getBuyDetail() {
        BuyGuankanquanTask task = new BuyGuankanquanTask("10", "1", quotaId, "0");
        task.addListener(new MyListener(mContext));
        buyGuankanquanTask = getEatShowApp().getTaskManager().addTask(task);
    }

    class WeixinReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isSuccess = intent.getBooleanExtra(ProtocolConst.WEIXINPAY_SUCCESS, false);
            if (isSuccess) {
                finish();
            } else {

            }

            if (mWeiXinReceive != null) {
                unregisterReceiver(mWeiXinReceive);
                mWeiXinReceive = null;
            }
        }
    }

    private void doWeixinPay() {
        if (!msgApi.isWXAppSupportAPI() || !msgApi.isWXAppInstalled()) {
            ToastUtil.showMessage(mContext, R.string.need_weixin_app);
            return;
        }

        if (mWeiXinReceive == null) {
            mWeiXinReceive = new WeixinReceive();
            IntentFilter filter = new IntentFilter();
            filter.addAction(WXPayEntryActivity.WEIXIN_PAY_SUCCESS_ACTION);
            registerReceiver(mWeiXinReceive, filter);
        }
        new WeiXinPay(msgApi, ZhuanyunMoneyActivity.this, orderNo, orderBody, total_money);

//        finish();
    }

    private void dismissDialog() {
        if (mProgress != null && mProgress.isShowing()) {
            mProgress.dismiss();
            mProgress = null;
        }
    }

    class MyListener extends ShowToastTaskEventListener {

        private String s;

        public MyListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (useMingeTask == taskId) {
                if (data != null && !data.equals("0")) {
                    ToastUtil.showMessage(mContext, "使用名额成功");
                    btn.setVisibility(View.GONE);
                }
            }
            if (orderTaskId == taskId) {
                if (data != null) {
                    OrderInfo orderInfo = (OrderInfo) data;
                    dismissDialog();
                    if (state.equals("0"))
                        if (orderInfo != null) {
                            orderNo = orderInfo.getOrderNo();
                            doWeixinPay();
                        }
                }
            }
            if (buyGuankanquanTask == taskId) {
                if (data != null) {
                    BuyGuankanquanInfo info = (BuyGuankanquanInfo) data;

                    list = info.getList().getList();
                    for (int i = 0; i < list.size(); i++) {
                        String nickName = "";
                        try {
                            if (!TextUtils.isEmpty(list.get(i).nickName)) {
                                nickName = URLDecoder.decode(list.get(i).nickName, "utf-8");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String price = list.get(i).price;
                        String quotaId = list.get(i).quotaId;
                        String quotaSum = list.get(i).quotaSum;
                        String status = list.get(i).status;
                        if (quotaId.equals("2737f7a418bf41c998728e6a0f9f253d")) {
                            if (status.equals("1")) {
                                s = nickName + "以" + price + "元寄售了" + quotaSum + "张观看券";
                            } else {
                                s = nickName + "以" + price + "元预购了" + quotaSum + "张观看券";
                            }
                        } else if (quotaId.equals("3d9513eb1b674af98de226b315765e3f")) {
                            if (status.equals("1")) {
                                s = nickName + "以" + price + "元寄售了" + quotaSum + "张观看券";
                            } else {
                                s = nickName + "以" + price + "元预购了" + quotaSum + "张观看券";
                            }
                        }
                        text_list.add(s);
                    }
                    TextView textView = new TextView(mContext);
                    if (text_list != null && text_list.size() > 0) {
                        for (int i = 0; i < text_list.size(); i++) {
                            textView.setText(text_list.get(i));
                            textView.setTextColor(getResources().getColor(R.color.black));
                            textView.setTextSize(15);
                            textView.setPadding(10, 10, 10, 10);
                            textView.setGravity(Gravity.CENTER_VERTICAL);
                            stepVvp.addView(textView);
                        }
                    } else {
                        textView.setText("暂无公告");
                        textView.setTextColor(getResources().getColor(R.color.black));
                        textView.setTextSize(15);
                        textView.setPadding(10, 10, 10, 10);
                        textView.setGravity(Gravity.CENTER_VERTICAL);
                        stepVvp.addView(textView);
                    }
                }
            }
            if (detailTaskId == taskId)
                if (data != null) {
                    userInfo = (EatShowUserInfo) data;
                    Log.e("userinfo", "" + userInfo.toString());
                    if (userInfo != null) {
                        String head_url = userInfo.getUserLogo();
                        vipCode = userInfo.getVipCode();
                        if (userInfo.getList() != null && userInfo.getList().size() > 0) {
                            for (int i = 0; i < userInfo.getList().size(); i++) {
                                String xueyuan = userInfo.getList().get(i).xueyuan;
                                //转运
                                if (xueyuan.equals("3d9513eb1b674af98de226b315765e3f")) {
                                    zhuanyun_jihuo = userInfo.getList().get(i).jihuoMinge;
                                    //父母大学
                                } else if (xueyuan.equals("2737f7a418bf41c998728e6a0f9f253d")) {
                                    parent_jihuo = userInfo.getList().get(i).jihuoMinge;
                                }
                            }
                        }
                        if (userInfo.shouyi != null && userInfo.shouyi.size() > 0) {
                            for (int i = 0; i < userInfo.shouyi.size(); i++) {
                                String shenfen = userInfo.shouyi.get(i).quotaId;
                                if (shenfen.equals("3d9513eb1b674af98de226b315765e3f")) {
                                    zhuanyun_shouyi = userInfo.shouyi.get(i).quotaShouyi;
                                } else if (shenfen.equals("2737f7a418bf41c998728e6a0f9f253d")) {
                                    parent_shouyi = userInfo.shouyi.get(i).quotaShouyi;
                                }
                            }
                            if (type.equals("zhuanyun")) {
                                class_num_tv.setText(zhuanyun_shouyi);
                            }
                            if (type.equals("parent")) {
                                tody_price.setText(parent_shouyi);
                            }
                        }
                        if (userInfo.price != null && userInfo.price.size() > 0) {
                            for (int i = 0; i < userInfo.price.size(); i++) {
                                String shenfen = userInfo.price.get(i).shenfen;
                                if (shenfen.equals("3d9513eb1b674af98de226b315765e3f")) {
                                    zhuanyun_price = userInfo.price.get(i).price;
                                } else if (shenfen.equals("2737f7a418bf41c998728e6a0f9f253d")) {
                                    parent_price = userInfo.price.get(i).price;
                                }
                            }
                            if (type.equals("zhuanyun")) {
                                if (TextUtils.isEmpty(zhuanyun_price)) {
                                    return;
                                }
                                tody_price.setText("今日价  " + zhuanyun_price);
                                if (TextUtils.isEmpty(zhuanyun_jihuo)) {
                                    return;
                                }
                                double i = Double.parseDouble(zhuanyun_jihuo) * Double.parseDouble(zhuanyun_price);
                                zhuanyun_shizhi.setText(i + "");
                            }
                            if (type.equals("parent")) {
                                if (TextUtils.isEmpty(parent_price)) {
                                    return;
                                }
                                tody_price.setText("今日价  " + parent_price);
                                if (TextUtils.isEmpty(parent_jihuo)) {
                                    return;
                                }
                                double i = Double.parseDouble(parent_jihuo) * Double.parseDouble(parent_price);
                                zhuanyun_shizhi.setText(i + "");
                            }
                        }
                        if (userInfo.getList() != null && userInfo.getList().size() > 0) {
                            for (int i = 0; i < userInfo.getList().size(); i++) {
                                String xueyuan = userInfo.getList().get(i).xueyuan;
                                if (xueyuan.equals("3d9513eb1b674af98de226b315765e3f")) {
                                    z_minge = userInfo.getList().get(i).minge;
//                                    if (userInfo.getList().get(i).shenfen.equals("0")) {
//                                        z_minge = userInfo.getList().get(i).minge;
//                                    } else if (userInfo.getList().get(i).shenfen.equals("1")) {
//                                        z_minge = userInfo.getList().get(i).minge;
//                                    } else if (userInfo.getList().get(i).shenfen.equals("2")) {
//                                        z_minge = userInfo.getList().get(i).minge;
//                                    } else if (userInfo.getList().get(i).shenfen.equals("3")) {
//                                        z_minge = userInfo.getList().get(i).minge;
//                                    }

                                } else if (xueyuan.equals("2737f7a418bf41c998728e6a0f9f253d")) {
                                    p_minge = userInfo.getList().get(i).minge;
//                                    if (userInfo.getList().get(i).shenfen.equals("0")) {
//                                        p_minge = userInfo.getList().get(i).minge;
//                                    } else if (userInfo.getList().get(i).shenfen.equals("1")) {
//                                        p_minge = userInfo.getList().get(i).minge;
//                                    } else if (userInfo.getList().get(i).shenfen.equals("2")) {
//                                        p_minge = userInfo.getList().get(i).minge;
//                                    } else if (userInfo.getList().get(i).shenfen.equals("3")) {
//                                        p_minge = userInfo.getList().get(i).minge;
//                                    }
                                }
                            }
                            if (type.equals("zhuanyun")) {
                                ming_e_tv.setText(z_minge);

                            } else if (type.equals("parent")) {
                                ming_e_tv.setText(p_minge);

                            }
                        }
                        if (userInfo.getIdentityList() != null && userInfo.getIdentityList().size() > 0) {
                            for (int i = 0; i < userInfo.getIdentityList().size(); i++) {
                                String xueyuan = userInfo.getIdentityList().get(i).xueyuan;
                                //转运
                                if (xueyuan.equals("3d9513eb1b674af98de226b315765e3f")) {
                                    shenfen1 = userInfo.getIdentityList().get(i).shenfen;
                                    if (shenfen1.equals("0")) {
                                        zhuanyun_yz = "院长";
                                    } else if (shenfen1.equals("1")) {
                                        zhuanyun_zr = "主任";
                                    } else if (shenfen1.equals("2")) {
                                        zhuanyun_ds = "导师";
                                    } else if (shenfen1.equals("3")) {
                                        zhuanyun_xy = "学员";
                                    }
                                } else if (xueyuan.equals("2737f7a418bf41c998728e6a0f9f253d")) {
                                    shenfen2 = userInfo.getIdentityList().get(i).shenfen;
                                    if (shenfen2.equals("0")) {
                                        p_zhuanyun_yz = "院长";
                                    } else if (shenfen2.equals("1")) {
                                        p_zhuanyun_zr = "主任";
                                    } else if (shenfen2.equals("2")) {
                                        p_zhuanyun_ds = "导师";
                                    } else if (shenfen2.equals("3")) {
                                        p_zhuanyun_xy = "学员";
                                    }
                                }
                            }
                            if (type.equals("zhuanyun") && !TextUtils.isEmpty(shenfen1)) {
//                                parent.removeAllViews();
                                initDescPopWindow();
                                return;
                            } else if (type.equals("parent") && !TextUtils.isEmpty(shenfen2)) {
//                                parent.removeAllViews();
                                initDescPopWindow();
                                return;
                            }
                        }

                    }

                }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }

}
